﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MyWebUtils;
using TTQT;
using TTQT.TTQT_Code;
public partial class DMHangHoa : MasterControl//System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblError.Text = string.Empty;
        if (!Page.IsPostBack)
        {
            bindGrid();
            visiblePanels(true, false);
            txtGiaBan.Attributes.Add("onkeyup", "formatTien(" + txtGiaBan.ClientID + ");");
        }
    }
    private void bindGrid()
    {
        try
        {
            ds.SelectCommandType = SqlDataSourceCommandType.Text;
            ds.SelectCommand = "select * from DM_Hang where maCn='" + UserInfo.Brn_Code + "' order by id desc";
            datagrid.DataSourceID = "ds";
            datagrid.DataBind();
        }
        catch (Exception exx)
        {
        }
    }
    private void visiblePanels(bool visibleGrid, bool visibleEdit)
    {
        this.pnGroupGrid.Visible = visibleGrid;
        this.pnEdit.Visible = visibleEdit;
    }
    protected void cmdAddNew_Click(object sender, EventArgs e)
    {
        txtMaHang.Enabled = true;
        lblError.Text = "";
        lblUpdateStatus.Text = "";
        visiblePanels(false, true);
        //this.txtMaHang.Text = string.Empty;
        //this.txtTenHang.Text = string.Empty;
    }

    protected void cmdUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtMaHang.Text.ToUpper().Contains("_KH"))
            {
                MyWebUtil.alert(this.Page, "Tên PGD không được chứa chuỗi _KH");
                return;
            }
        }
        catch { }
        try
        {
            // Nếu là sửa Thì ID sẽ khác rỗng

            if (!txtMaHang.Enabled)
            {
                HangHoa h = new HangHoa();
                h.id = txtID.Text;
                h.getByID();
                h.tenHang = txtTenHang.Text;
                h.donViTinh = txtDonVi.Text;
                h.tonThap = txtTonThap.Text;
                h.tonCao = txtTonCao.Text;
                h.giaBan = txtGiaBan.Text.Replace(",", "");
                h.khuyenMai = txtKhuyenMai.Text;
                //h.active = "1";
                //h.maCN = UserInfo.Brn_Code;
                if (h.update() > 0)
                {
                    bindGrid();
                    visiblePanels(true, false);
                    lblError.Text = string.Empty;
                }

            }
            // Nếu là thêm mới thì ID sẽ rỗng
            else
            {
                HangHoa h = new HangHoa();
                h.maHang = txtMaHang.Text;
                h.tenHang = txtTenHang.Text;
                h.donViTinh = txtDonVi.Text;
                h.tonThap = txtTonThap.Text;
                h.tonCao = txtTonCao.Text;
                h.giaBan = txtGiaBan.Text.Replace(",", "");
                h.khuyenMai = txtKhuyenMai.Text;
                h.active = "1";
                h.maCN = UserInfo.Brn_Code;
                if (h.insert() > 0)
                    bindGrid();
                visiblePanels(true, false);
            }
        }
        catch (Exception ex)
        {
            lblUpdateStatus.Attributes.Add("style", "color:red;font-weight:bold;");
            this.lblUpdateStatus.Text = "Trung ma. Nhap lai! <br />" + ex.ToString();
            //Log methods here
        }
    }
    protected void cmdCancel_Click(object sender, EventArgs e)
    {
        lblError.Text = "";
        lblUpdateStatus.Text = "";
        visiblePanels(true, false);
    }
    protected void datagrid_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        if (e.CommandName == "cmdEdit")
        {
            txtID.Text = e.Item.Cells[0].Text;
            
            HangHoa h=new HangHoa();
            h.id = e.Item.Cells[0].Text;
            h.getByID();
            txtMaHang.Text = h.maHang;
            txtTenHang.Text = h.tenHang;
            txtDonVi.Text = h.donViTinh;
            txtTonThap.Text = h.tonThap;
            txtTonCao.Text = h.tonCao;
            txtGiaBan.Text = h.giaBan;
            txtGiaBan.Text = TTQTCommon.formatTien_KhongThapPhan(txtGiaBan.Text);
            txtKhuyenMai.Text = h.khuyenMai;




            visiblePanels(false, true);

            txtMaHang.Enabled = false;
        }
        else if (e.CommandName == "cmdDelete")
        {
            DataView kq;
            ds.SelectCommand = "select count(*) from hoadon_detail where khoxuat='" + e.Item.Cells[0].Text + "' or KhoNhan= '" +
                               e.Item.Cells[0].Text + "'";
            kq = (DataView)ds.Select(System.Web.UI.DataSourceSelectArguments.Empty);

            if (!kq.Table.Rows[0][0].ToString().Equals("0"))
            {
                MyWebUtil.alert(this.Page, "Đã có giao dịch xuất (nhập) liên quan nên không xóa được.");
                bindGrid();
                return;
            }

            ds.SelectCommand = "Select count(*) from users where brn_Code='" + e.Item.Cells[0].Text + "'";
            kq = (DataView)ds.Select(System.Web.UI.DataSourceSelectArguments.Empty);

            if (!kq.Table.Rows[0][0].ToString().Equals("0"))
            {
                MyWebUtil.alert(this.Page, "Bạn phải xóa các user thuộc PGD này trước khi xóa PGD");
                bindGrid();
                return;
            }
            ds.DeleteCommand = "delete MaKho where MaKho='" + e.Item.Cells[0].Text + "'";
            ds.Delete();
            ds.DeleteCommand = "delete BrnStatus where BranchCode='" + e.Item.Cells[0].Text + "'";
            ds.Delete();

            bindGrid();

        }
    }
    protected void datagrid_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex < 0) return;
        Button cmdDel = (Button)e.Item.FindControl("cmdDelete");
        if (cmdDel != null) cmdDel.Attributes.Add("onclick", TTQTCommon.delete_Confirm);
    }
    protected void datagrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        datagrid.CurrentPageIndex = e.NewPageIndex;
        bindGrid();
    }
}
